package com.example.demo.controller;

import com.example.demo.annotation.Api;
import com.example.demo.domain.PageResult;
import com.example.demo.domain.exam.Exam;
import com.example.demo.domain.exam.ExamUserDelay;
import com.example.demo.domain.exam.QueryModel;
import com.example.demo.domain.exam.condition.ExamCondition;
import com.example.demo.dto.DelayTimeDTO;
import com.example.demo.dto.ExamUserSingleQuesScoreDTO;
import com.example.demo.dto.ExamClassStaffDTO;
import com.example.demo.dto.OneClickReviewDTO;
import com.example.demo.dto.RepeatDTO;
import com.example.demo.dto.ResetIpDTO;
import com.example.demo.exception.ExceptionUtils;
import com.example.demo.service.VojObjectiveQuestionService;
import com.example.demo.service.exam.ExamService;
import com.example.demo.service.exam.ExamUserDelayService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@RequiredArgsConstructor
@RequestMapping("/exam/")
public class ExamController {

    private final ExamService examService;

    private final VojObjectiveQuestionService vojObjectiveQuestionService;

    private final ExamUserDelayService examUserDelayService;

    @Api
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public void createExam(@RequestBody Exam exam) {
        examService.createExam(exam);
    }

    @Api
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public List<ExamClassStaffDTO> getExamStaff(@RequestParam("id") long id) {
        return examService.getExamStaff(id);
    }

    @Api
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResult<Exam> page(@RequestBody QueryModel<ExamCondition> query) {
        return examService.page(query);
    }

    @Api
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateExam(@RequestBody Exam exam) {
        examService.updateExam(exam);
    }

    @Api
    @RequestMapping(value = "/review", method = RequestMethod.POST)
    public void reviewExamPaper(@RequestBody ExamUserSingleQuesScoreDTO dto) {
        examService.reviewExamPaper(dto);
    }

    @Api
    @RequestMapping(value = "/one-click/review", method = RequestMethod.POST)
    public void oneClickReview(@RequestBody OneClickReviewDTO dto) {
        examService.oneClickReview(dto);
    }

    @Api
    @RequestMapping(value = "/delay", method = RequestMethod.POST)
    public void delayExamTimeForUser(@RequestBody ExamUserDelay examUserDelay) {
        examUserDelayService.insert(examUserDelay);
    }

    /**
     * 重置某场考试考生ip
     * @param dto 参数
     */
    @Api
    @RequestMapping(value = "/ip/reset", method = RequestMethod.POST)
    public void resetIp(@RequestBody ResetIpDTO dto) {
        examService.resetIp(dto);
    }

    /**
     * 更新考生某场考试延时时间
     * @param examUserDelay 延时参数
     */
    @Api
    @RequestMapping(value = "/delay/update", method = RequestMethod.POST)
    public void delayExamTimeUpdate(@RequestBody ExamUserDelay examUserDelay) {
        examUserDelayService.update(examUserDelay);
    }

    /**
     * 删除用户考试延时
     * @param id
     */
    @Api
    @RequestMapping(value = "/delay/delete", method = RequestMethod.POST)
    public void delayExamTimeDelete(@RequestParam Long id) {
        examUserDelayService.delete(id);
    }

    /**
     * 获取用户考试延时
     * @param dto
     */
    @Api
    @RequestMapping(value = "/delay/get", method = RequestMethod.POST)
    public void delayExamTimeGet(@RequestBody DelayTimeDTO dto) {
        Long examId = Optional.ofNullable(dto.getExamId()).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        Long userId = Optional.ofNullable(dto.getUserId()).orElseThrow(() -> ExceptionUtils.exception("用户标识不能为空"));
        examUserDelayService.getByExamUserId(examId, userId);
    }

    /**
     * 重置某个考生某场考试
     * @param dto 查询实体
     */
    @Api
    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    public void resetExam(@RequestBody DelayTimeDTO dto) {
        Long examId = Optional.ofNullable(dto.getExamId()).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        Long userId = Optional.ofNullable(dto.getUserId()).orElseThrow(() -> ExceptionUtils.exception("用户标识不能为空"));
        examService.resetExam(examId, userId);
    }

    /**
     * 批量阅卷
     * @param examId 考试标识
     */
    @Api
    @RequestMapping(value = "/batch/one-click/review", method = RequestMethod.POST)
    public void batchOneClickReview(@RequestParam Long examId) {
        examService.batchOneClickReview(examId);
    }

    @Api
    @RequestMapping(value = "/repeat/get", method = RequestMethod.POST)
    public List<RepeatDTO> getExamRepeat(@RequestParam Long examId) {
        return examService.getExamRepeat(examId);
    }

}
